﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Stg;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.DALFactory;
using System.Linq.Dynamic;
namespace HKEA.CMS.Core.DAL.Stg
{
    public class stg_tblCustomerDAL:Istg_tblCustomer
    {
        HKEA_CMSEntities model;
        stg_tblCustomer customer;

        public stg_tblCustomerDAL()
        {
            model = DataAccess.GetModelInstance();
        }

        public static stg_tblCustomerDAL GetInstance()
        {
            return new stg_tblCustomerDAL();
        }

        public int InsertENT(stg_tblCustomer ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.stg_tblCustomer.AddObject(ent);
            return model.SaveChanges();
        }

        public List<stg_tblCustomer> GetListByENT(stg_tblCustomer ent, PageParam param, string strPropertyName,string strOrderBy)
        {
            var result = model.stg_tblCustomer.Where(a => a.CustomerUID > 0);
            if (ent.CustomerUID > 0)
                result = result.Where(a => a.CustomerUID == ent.CustomerUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            param.Count = result.Count();
            result = result.OrderBy(strPropertyName + " " + strOrderBy).Skip((param.CurrentPage - 1) * param.PageSize).Take(param.PageSize);
            return result.ToList();
        }
        public List<stg_tblCustomer> GetListByENT(stg_tblCustomer ent)
        {
            var result = model.stg_tblCustomer.Where(a => a.CustomerUID > 0);
            if (ent.CustomerUID > 0)
                result = result.Where(a => a.CustomerUID == ent.CustomerUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.ToList();
        }
        public int GetCountByENT(stg_tblCustomer ent)
        {
            var result = model.stg_tblCustomer.Where(a => a.CustomerUID > 0);
            if (ent.CustomerUID > 0)
                result = result.Where(a => a.CustomerUID == ent.CustomerUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.Count();
        }

        public int UpdateENT(stg_tblCustomer ent)
        {
            customer = model.stg_tblCustomer.Where(a=>a.CustomerUID==ent.CustomerUID).FirstOrDefault();
            if (customer != null)
            {
                customer.LastModifyBy = ent.LastModifyBy;
                customer.LastModifyDate = DateTime.Now;
                customer.AddressUID = ent.AddressUID;
                customer.BR_HKID = ent.BR_HKID;
                customer.ContactInfoUID = ent.ContactInfoUID;
                customer.CustomerID = ent.CustomerID;
                customer.CustTypeUID = ent.CustTypeUID;
                customer.EntryDate = ent.EntryDate;
                customer.EstDate = ent.EstDate;
                customer.HQLocatedinHK = ent.HQLocatedinHK;
                customer.IsGCMember = ent.IsGCMember;
                customer.IsMember = ent.IsMember;
                customer.IsVIP = ent.IsVIP;
                customer.Name_Chi = ent.Name_Chi;
                customer.Name_Eng = ent.Name_Eng;
                customer.NoOfEmp = ent.NoOfEmp;
                customer.Stroke = ent.Stroke;
                customer.Terminated = ent.Terminated;
                customer.WebSite = ent.WebSite;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(stg_tblCustomer ent)
        {
            var temp = model.stg_tblCustomer.Where(a => a.CustomerUID > 0);
            if (ent.CustomerUID > 0)
                temp = temp.Where(a=>a.CustomerUID==ent.CustomerUID);
            var list = temp.ToList();
            for (int i = 0; i < list.Count(); i++)
            {
                model.stg_tblCustomer.DeleteObject(list[i]);
            }
            return model.SaveChanges();
        }
    }
}
